package com.mazegame.frontend;

import com.mazegame.backend.BackgroundPanel;
import javafx.animation.*;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.effect.BlendMode;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.util.Duration;

import javax.swing.*;
import java.awt.*;
import java.util.Random;


public class EndPanel extends JPanel implements GamePanel{
    private CardLayout cardLayout;
    private JPanel cardPanel;
    private JLayeredPane layeredPane;
    private static final int WIDTH = 1500;
    private static final int HEIGHT = 900;
    private JFXPanel jfxPanel;
    private final Random random = new Random();


    public EndPanel(CardLayout cardLayout, JPanel cardPanel) {
        this.cardLayout = cardLayout;//
        this.cardPanel = cardPanel;
        initializeUI();
    }

    private void initializeUI() {
        // 使用 JLayeredPane 管理不同层级的组件
        layeredPane = new JLayeredPane();
        setLayout(new BorderLayout());
        add(layeredPane, BorderLayout.CENTER);

        // 创建背景面板并添加到最底层
        BackgroundPanel backgroundPanel = new BackgroundPanel("/img/待机.jpg");
        backgroundPanel.setBounds(0, 0, WIDTH, HEIGHT);
        layeredPane.add(backgroundPanel, JLayeredPane.DEFAULT_LAYER);

        // 创建 JavaFX 内容面板（登录界面）
        jfxPanel = new JFXPanel();
        jfxPanel.setOpaque(false); // 确保 JFXPanel 透明
        jfxPanel.setBounds(0, 0, WIDTH, HEIGHT);
        layeredPane.add(jfxPanel, JLayeredPane.MODAL_LAYER); // 放在上层

        Platform.runLater(() -> {
            // 创建主容器
            Pane root = createContentPane();
            //设置root透明
            root.setStyle("-fx-background-color: rgba(0, 0, 0, 0);");
            // 创建透明场景
            Scene scene = new Scene(root, WIDTH, HEIGHT, Color.TRANSPARENT);
            jfxPanel.setScene(scene);
        });
    }

    private Pane createContentPane() {
        Pane root = new Pane();
        VBox vBox = new VBox();
        vBox.setSpacing(20);
        // 创建通关文本
        Text clearText = new Text("恭喜通关成功！");
        clearText.setFont(Font.font("Arial", 64));
        clearText.setFill(Color.RED);
        clearText.setTranslateX(WIDTH / 2 - clearText.getLayoutBounds().getWidth() / 2);
        clearText.setTranslateY(HEIGHT / 2 - 50);

        // 创建按钮
        Button rankingBtn = new Button("查看排名");
        rankingBtn.setFont(Font.font("KaiTi", javafx.scene.text.FontWeight.BOLD, 32));
        rankingBtn.setPrefWidth(200);
        rankingBtn.setPrefHeight(60);
        rankingBtn.setOnAction(e -> cardLayout.show(cardPanel, "RANKPANEL"));

        Button backBtn = new Button("返回");
        backBtn.setFont(Font.font("KaiTi", javafx.scene.text.FontWeight.BOLD, 32));
        backBtn.setPrefWidth(200);
        backBtn.setPrefHeight(60);
        backBtn.setOnAction(e -> cardLayout.show(cardPanel, "MAINPANEL"));

        // 按钮布局
        HBox buttonBox = new HBox(20);
        buttonBox.getChildren().addAll(rankingBtn, backBtn);
        buttonBox.setTranslateX(WIDTH / 3 - buttonBox.getLayoutBounds().getWidth() / 2+20);
        buttonBox.setTranslateY(HEIGHT / 2 + 50);

        root.getChildren().addAll( clearText, buttonBox);


        // 添加彩带效果
        createConfetti(root);

        return root;
    }

    private void createConfetti(Pane root) {
        for (int i = 0; i < 100; i++) {
            javafx.scene.shape.Rectangle confetti = new Rectangle(random.nextInt(5) + 5, random.nextInt(20) + 10);
            confetti.setFill(Color.rgb(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
            confetti.setBlendMode(BlendMode.SRC_OVER);
            confetti.setTranslateX(random.nextInt(WIDTH));
            confetti.setTranslateY(-random.nextInt(HEIGHT));

            // 创建下落动画
            TranslateTransition fall = new TranslateTransition(Duration.seconds(random.nextDouble() * 3 + 2), confetti);
            fall.setToY(HEIGHT);

            // 创建左右摇摆动画
            Timeline sway = new Timeline(
                    new KeyFrame(Duration.ZERO, new KeyValue(confetti.translateXProperty(), confetti.getTranslateX())),
                    new KeyFrame(Duration.seconds(random.nextDouble() + 1),
                            new KeyValue(confetti.translateXProperty(), confetti.getTranslateX() + random.nextInt(60) - 30))
            );
            sway.setCycleCount(Animation.INDEFINITE);
            sway.setAutoReverse(true);
            sway.play();

            // 创建旋转动画
            RotateTransition rotate = new RotateTransition(Duration.seconds(random.nextDouble() * 2 + 1), confetti);
            rotate.setByAngle(random.nextInt(360));
            rotate.setCycleCount(Animation.INDEFINITE);
            rotate.play();

            root.getChildren().add(confetti);
            fall.play();

            // 动画结束后重置位置
            fall.setOnFinished(event -> {
                confetti.setTranslateY(-random.nextInt(HEIGHT));
                fall.playFromStart();
            });
        }
    }

    @Override
    public void showPanel() {
        cardLayout.show(cardPanel, "ENDPANEL");
    }

    @Override
    public void cleanup() {

    }
}
